package org.moonshiner.controller.service.impl;

import java.util.List;

import org.moonshiner.controller.dao.UserDao;
import org.moonshiner.controller.dao.hibernate.PgUserDaoImpl;
import org.moonshiner.controller.service.UserService;
import org.moonshiner.model.project.Project;
import org.moonshiner.model.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl extends GenericService<PgUserDaoImpl, User>
		implements UserService {

	@Autowired
	public UserServiceImpl(PgUserDaoImpl dao) {
		super(dao);
	}

	@Autowired
	private UserDao dao;

	@Autowired
	private UserAssebmler assebmler;

	@Transactional
	@Override
	public UserDetails loadUserByUsername(String name)
			throws UsernameNotFoundException {
		User user = getDao().getUserByUserName(name);

		if (null == user) {
			throw new UsernameNotFoundException("User was not found");
		}
		return assebmler.buildUser(user);
	}

	@Override
	@Transactional
	public User getUserByName(String name) {
		return getDao().getUserByUserName(name);
	}

	@Override
	@Transactional
	public List<Project> getProjectsByUserName(String name) {
		return getDao().getProjectsByUserName(name);
	}

	@Override
	@Transactional
	public List<Project> getProjectsByUserId(Integer id) {
		return getDao().getProjectsByUserId(id);
	}

}
